<template>
  <el-dialog title="分配角色" :visible="showDialog" @close="closeFn">
    <el-checkbox-group v-model="checkedList">
      <!-- 多选框容器 -->
      <!-- 备选中的值储存在 label 中 -->
      <el-checkbox
        v-for="role in roleList"
        :key="role.id"
        :label="role.id"
      >
        {{ role.name }}
      </el-checkbox>
    </el-checkbox-group>
    <!-- footer -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
          <el-button size="small" @click="closeFn">取消</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getRoleList } from '@/api/setting'
import { getUserDetailById } from '@/api/user'
import { assignRoles } from '@/api/employee'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 全部角色列表
      roleList: [],
      // 选中的角色列表
      checkedList: [],
      userId: ''
    }
  },

  async created() {
    // 获取角色列表
    const { rows } = await getRoleList()
    this.roleList = rows
    // console.log(this.roleList)
  },

  methods: {
    // 获取用户详情
    async getUserDetail(id) {
      // 解构出roleIds
      const { roleIds } = await getUserDetailById(id)
      this.checkedList = roleIds
      this.userId = id
      // console.log(roleIds, this.checkedList)
    },

    // 保存修改分配角色
    async btnOK() {
      // 传入参数
      await assignRoles({ roleIds: this.checkedList, id: this.userId })
      this.$emit('update:showDialog', false)
      this.$message.success('修改成功!')
    },

    // 关闭弹窗
    closeFn() {
      // 清除选中
      this.checkedList = []
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>

</style>
